package com.gladlee.ACP;

import static com.gladlee.ACP.tool.AtomTool.findFreePort;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import com.gladlee.myACP.myAtomUser;

public class Atom {
	protected DatagramSocket socket;
	protected DatagramPacket packet_r;
	protected DatagramPacket packet_s;
	protected int bufferSize = 8192;
	protected int port;
	protected InetAddress address;
	protected Map<String, byte[]> ADFIn;
	protected Map<String, byte[]> ADFOut;
	protected String encoding;
	protected AtomUser user;
	protected Thread sender;
	protected Thread receiver;

	public Atom() {
//		user.e
		encoding = "UTF-8";
		port = findFreePort();
		ADFIn = new HashMap<String, byte[]>();
		ADFOut = new HashMap<String, byte[]>();
		// address=null;
		try {
			// address = InetAddress.getByName(getLocalIP());
			socket = new DatagramSocket(port);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Atom(int pt) {
		this();
		port = pt;
	}

	public Atom(String ip) {
		this();
		try {
			address = InetAddress.getByName(ip);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public Atom(String ip, int pt) {
		this();
		port = pt;
		try {
			address = InetAddress.getByName(ip);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}

	public void close() {

	}

	public void regUser(AtomUser u) {
		user = u;
		user.setAtom(this);
	};

	public int getIntData(String cc) {
		return user.getIntData(cc, ADFIn);
	}

	public double getDoubleData(String cc) {
		return user.getDoubleData(cc, ADFIn);
	}

	public String getStringData(String cc) {
		return user.getStringData(cc, ADFIn);
	}

	private class Receiver implements Runnable {
		@Override
		public void run() {
			user.receive();
		}
	}

	private class Sender implements Runnable {

		@Override
		public void run() {
			user.send();
		}
	}

	public void work() {
		sender = new Thread(new Sender());
		receiver = new Thread(new Receiver());
		sender.start();
		receiver.start();
	}

	public void stop() {
		// sender.
	}

	public byte[] getDataBytes(String key) {
		byte[] data = ADFIn.get(key);
		ADFIn.remove(key);
		return data;
	}

	public static void main(String[] args) {
		Atom acp = new Atom("59.66.136.255");
		myAtomUser user = new myAtomUser();
		acp.regUser(user);
		acp.work();
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public InetAddress getAddress() {
		return address;
	}

	public void setAddress(InetAddress address) {
		this.address = address;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public AtomUser getUser() {
		return user;
	}

	public void setUser(AtomUser user) {
		this.user = user;
	}

	public DatagramPacket getPacket_r() {
		return packet_r;
	}

	public DatagramPacket getPacket_s() {
		return packet_s;
	}

	public DatagramSocket getSocket() {
		return socket;
	}

	public Map<String, byte[]> getADFIn() {
		return ADFIn;
	}

	public Map<String, byte[]> getADFOut() {
		return ADFOut;
	}
}
